package com.nf.health.app.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.adapter.NewsFragmentPagerAdapter;
import com.nf.health.app.adapter.NewsFragmentPagerAdapter2;
import com.nf.health.app.customview.ColumnHorizontalScrollView;
import com.nf.health.app.models.ChannelItem;
import com.nf.health.app.utils.Util;

import java.util.ArrayList;


/**
 * 订单  fragment
 * Created by windoes on 2016/3/17.
 */
public class OederFragment2 extends IBaseFragment {

    String flag;


    /** 自定义HorizontalScrollView */
    private ColumnHorizontalScrollView mColumnHorizontalScrollView;
    LinearLayout mRadioGroup_content;
    private ViewPager mViewPager2;
    /** 用户选择的新闻分类列表 */
    private ArrayList<ChannelItem> userChannelList2 = new ArrayList<ChannelItem>();
    /** 当前选中的栏目 */
    private int columnSelectIndex = 0;
    /** 屏幕宽度 */
    private int mScreenWidth = 0;
    /** Item宽度 */
    private int mItemWidth = 0;
    View view;
    TextView iv,tv,line;
    private ArrayList<Fragment> fragments2 = new ArrayList<Fragment>();
    private LayoutInflater mInflater;





    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        addViewFillInRoot(R.layout.fragment_order2);

        flag = getArguments().getString("type");
        mScreenWidth = Util.getWindowsWidth(getActivity());
        mInflater = LayoutInflater.from(getActivity());
        mColumnHorizontalScrollView = (ColumnHorizontalScrollView) getActivity().findViewById(R.id.mColumnHorizontalScrollView2);
        mRadioGroup_content = (LinearLayout) getActivity().findViewById(R.id.mRadioGroup_content2);
        mViewPager2 = (ViewPager) getActivity().findViewById(R.id.mViewPager2);
        initData();
    }

    private void initData() {
      if (flag.equals("2")) {//服务订单
            initView2();
        }

    }






    private void initView2() {

        mItemWidth = mScreenWidth/3;// 一个Item宽度为屏幕的1/3
        initColumnData2();
        initTabColumn2();
        initFragment2();
    }


    private void initColumnData2() {
        userChannelList2 = new ArrayList<ChannelItem>();
        ChannelItem item = new ChannelItem();
        item.setName("全部");
        item.setId(1);
        item.setOrderId(1);
        item.setSelected(1);
        userChannelList2.add(item);

        item = new ChannelItem();
        item.setName("待付款");
        item.setId(2);
        item.setOrderId(2);
        item.setSelected(2);
        userChannelList2.add(item);

        item = new ChannelItem();
        item.setName("退款");
        item.setId(3);
        item.setOrderId(3);
        item.setSelected(3);
        userChannelList2.add(item);
    }
    /**
     * 初始化Column栏目项
     * */
    private void initTabColumn2() {
        mRadioGroup_content.removeAllViews();
        int count = userChannelList2.size();
        mColumnHorizontalScrollView.setParam(getActivity(), mScreenWidth, mRadioGroup_content);
        for (int i = 0; i < count; i++) {
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    mItemWidth, AbsListView.LayoutParams.WRAP_CONTENT);
            params.leftMargin = 5;
            params.rightMargin = 5;
            view=mInflater.inflate(R.layout.itme, null);
            iv=(TextView) view.findViewById(R.id.iv);
            tv=(TextView) view.findViewById(R.id.tv);
            line=(TextView) view.findViewById(R.id.line);
            line.setBackgroundResource(R.drawable.orangeline);
            tv.setId(i);
            tv.setText(userChannelList2.get(i).getName());
            tv.setTextColor(getResources().getColorStateList(R.color.top_category_scroll_text_color_day));
            if (columnSelectIndex == i) {
                iv.setSelected(true);
                tv.setSelected(true);
                line.setSelected(true);
            }
            view.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    for (int i = 0; i < mRadioGroup_content.getChildCount(); i++) {
                        View localView = mRadioGroup_content.getChildAt(i);
                        if (localView != v)
                            localView.setSelected(false);
                        else {
                            localView.setSelected(true);
                            mViewPager2.setCurrentItem(i);
                        }
                    }
                }
            });
            mRadioGroup_content.addView(view, i,params);
        }
    }

    /**
     * 初始化Fragment
     * */
    private void initFragment2() {
        int count = userChannelList2.size();
        for (int i = 0; i < count; i++) {
            Bundle data = new Bundle();
            data.putString("text", userChannelList2.get(i).getName());
            data.putInt("id", userChannelList2.get(i).getId());
            CountingFragment newfragment = new CountingFragment();
            newfragment.setArguments(data);
            fragments2.add(newfragment);
        }
        NewsFragmentPagerAdapter2 mAdapetr2 = new NewsFragmentPagerAdapter2(
                getActivity().getSupportFragmentManager(), fragments2);


        mViewPager2.setOffscreenPageLimit(0);
        mViewPager2.setAdapter(mAdapetr2);
        mViewPager2.setOnPageChangeListener(pageListener2);
    }

    /**
     * ViewPager切换监听方法
     * */
    public ViewPager.OnPageChangeListener pageListener2 = new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int position) {
            Log.e("position==", "" + position);
            mViewPager2.setCurrentItem(position);
            selectTab(position);
            initTabColumn2();
        }
    };

    /**
     * 选择的Column里面的Tab
     * */
    private void selectTab(int tab_postion) {
        columnSelectIndex = tab_postion;
        for (int i = 0; i < mRadioGroup_content.getChildCount(); i++) {
            View checkView = mRadioGroup_content.getChildAt(tab_postion);
            int k = checkView.getMeasuredWidth();
            int l = checkView.getLeft();
            int i2 = l + k / 2 - mScreenWidth / 2;
            // rg_nav_content.getParent()).smoothScrollTo(i2, 0);
            mColumnHorizontalScrollView.smoothScrollTo(i2, 0);
            // mColumnHorizontalScrollView.smoothScrollTo((position - 2) *
            // mItemWidth , 0);
        }
        // 判断是否选中
        for (int j = 0; j < mRadioGroup_content.getChildCount(); j++) {
            View checkView = mRadioGroup_content.getChildAt(j);
            boolean ischeck;
            if (j == tab_postion) {
                ischeck = true;
            } else {
                ischeck = false;
            }
            checkView.setSelected(ischeck);
        }
    }

}

